<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/25
 * Time: 14:55
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
use Qiniu\Auth;// 引入鉴权类
use Qiniu\Storage\UploadManager;// 引入上传类
use Qiniu\Processing\PersistentFop;
require_once('../extend/qiniu/autoload.php');
class Famous extends Base {
    public function index(){
        if (request()->isAjax()) {
            extract(input());
            $map =array();
            if(isset($keyword)&&$keyword!="")
            {
                $map['video_name'] = ['like',"%" . $keyword . "%"];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('person_video')->alias('a')
                ->join('up_person up','up.person_id = a.person_id','left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('person_video')->alias('a')
                ->join('up_person up','up.person_id = a.person_id','left')
                ->page($NowPage, $limits)
                ->field('a.*,up.person_name')
                ->where($map)
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['is_down']==1){
                    $vo['is_down_name'] = '可以下载';
                }else{
                    $vo['is_down_name'] = '不可以下载';
                }
                if($vo['is_vip']==1){
                    $vo['is_vip_name'] = '普通';
                }else{
                    $vo['is_vip_name'] = 'Vip';
                }
                if($vo['is_index_vip']==1){
                    $vo['is_index_vip_name'] = '是';
                }else if($vo['is_index_vip']==2){
                    $vo['is_index_vip_name'] = '否';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?video_id1')){
            session('video_id1',null);
        }
        return $this->fetch();
    }
    public function add(){
        if(Request::instance()->isPost()){
            $video_name = input('param.video_name');
            $person_id = input('param.person_id');
            $video_performer = input('param.video_performer');
            $is_down = input('param.is_down');
            $is_vip = input('param.is_vip');
            $video_lp= input('param.video_lp');
            $video_year = input('param.video_year');
            $video_img = input('param.video_img');
            $video_intro= input('param.video_intro');
            $data['video_name'] = $video_name;
            $data['person_id'] = $person_id;
            $data['video_performer'] = $video_performer;
            $data['is_down'] = $is_down;
            $data['is_vip'] = $is_vip;
            $data['video_lp'] = $video_lp;
            $data['video_year'] = $video_year;
            $data['video_img'] = $video_img;
            $data['video_intro'] = $video_intro;
            $data['video_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_person_video')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $info2 = Db::table('up_person')->select();
        $this->assign('info2',$info2);
        $info3 = Db::table('up_school')->select();
        $this->assign('info3',$info3);
        return $this->fetch();
    }
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $video_name = input('param.video_name');
            $person_id = input('param.person_id');
            $video_performer = input('param.video_performer');
            $is_down = input('param.is_down');
            $is_vip = input('param.is_vip');
            $video_lp= input('param.video_lp');
            $video_year = input('param.video_year');
            $video_img = input('param.video_img');
            $video_intro= input('param.video_intro');
            $data['video_name'] = $video_name;
            $data['person_id'] = $person_id;
            $data['video_performer'] = $video_performer;
            $data['is_down'] = $is_down;
            $data['is_vip'] = $is_vip;
            $data['video_lp'] = $video_lp;
            $data['video_year'] = $video_year;
            $data['video_intro'] = $video_intro;
            if(!empty($video_img)){
                $data['video_img'] = $video_img;
            }
            $res = Db::table('up_person_video')->where('video_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_person_video')->where('video_id',$id)->find();
        $info2 = Db::table('up_person')->select();
        $this->assign('id',$id);
        $this->assign('res',$res);
        $this->assign('info2',$info2);
        $info3 = Db::table('up_school')->select();
        $this->assign('info3',$info3);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     *
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('person_video')->where('video_id',$task_id)->setField('is_index_vip',1);
        if($res){
            return json(['code'=>200,'msg'=>'指定成功']);
        }else{
            return json(['code'=>100,'msg'=>'指定失败']);
        }
    }
    /***
     * @return \think\response\Json
     *
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('person_video')->where('video_id',$task_id)->setField('is_index_vip',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function index1(){
        if (request()->isAjax()) {
            extract(input());
            $where['f_id'] = input('param.id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('person_chirden')->alias('a')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('person_chirden')->alias('a')
                ->page($NowPage, $limits)
                ->where($where)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?video_id1')){
            session('video_id1',input('id'));
        }
        return $this->fetch();
    }

    public function add1(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['video'])){
                return json(['code' => 100, 'msg' => '请上传视频']);
            }
            //转码的文件url
            $url = "http://qiniu2.verynurse.com/" . $params['video'];//未转码之前的视频url，用于获取时长
            //时长
            $a = file_get_contents($url . '?avinfo');
            $b = json_decode($a,true);
            $time = explode('.', $b['streams'][0]['duration']);
            $fen = floor($time[0]/60);
            $miao = floor(($time[0]-$fen*60)%60);
            $this->zhuanma($params['video']);
            $data = [
                'chirden_title' => $params['chirden_title'],//标题
                'chirden_url' => "http://qiniu2.verynurse.com/" . 'ndgl_' . $params['video'],//视频
                'f_id' => session('video_id1'),//视频ID
                'chirden_time' => date('Y-m-d H:i:s'),//创建时间
                'catalogue_time' => $fen . ':' . $miao//视频时长
            ];
            $res = Db::table('up_person_chirden')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    public function update1(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['video'])){
                return json(['code' => 100, 'msg' => '请上传视频']);
            }
            $old_url = Db::table('up_video_chirden')->where('chirden_id', $params['id'])->value('chirden_url');
            //转码的文件url
            //有视频但是和原视频地址不同
            if($params['video'] != '' && $params['video'] != $old_url){
                $url = "http://qiniu2.verynurse.com/" . $params['video'];//未转码之前的视频url，用于获取时长
                //时长
                $a = file_get_contents($url . '?avinfo');
                $b = json_decode($a,true);
                $time = explode('.', $b['streams'][0]['duration']);
                $fen = floor($time[0]/60);
                $miao = floor(($time[0]-$fen*60)%60);
                $this->zhuanma($params['video']);
                $data['chirden_url'] = "http://qiniu2.verynurse.com/" . 'ndgl_' . $params['video'];//视频地址
                $data['chirden_time'] = $fen . ':' . $miao;//视频时长
            }
            $data = [
                'chirden_title' => $params['chirden_title'],//标题
            ];
            $res = Db::table('up_person_chirden')->where('chirden_id',$params['id'])->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_video_chirden')->where('chirden_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$res);
        return $this->fetch();
    }

    /**
     * 转码
     * @param $key
     */
    public function zhuanma($key){
        set_time_limit (0);
        $accessKey = config('qiniu')['accessKey'];
        $secretKey = config('qiniu')['secretKey'];
        $bucket = config('qiniu')['bucket'];
        $auth = new Auth($accessKey, $secretKey);
        $pipeline = 'nandinggeer';//多媒体队列名
        $force = false;
        //转码完成后通知到你的业务服务器。
//        $notifyUrl = '= 'http://cjtdcms.xuanwumobile.com/super#advert_manage';

        $config = new \Qiniu\Config();
        $pfop = new PersistentFop($auth, $config);
        $fops = "avthumb/mp4/vcodec/libx264/s/720x480|saveas/" . \Qiniu\base64_urlSafeEncode($bucket .  ":" . 'ndgl_' . $key);
        list($id, $err) = $pfop->execute($bucket, $key, $fops, $pipeline, '', $force);
        if($err !== null) {
            $res = $err;
        } else {
            $res = $id;
        }
    }

    public function up2(){
        $task_id = input('param.id');
        $res = Db::name('person_video')->where('person_id',$task_id)->setField('is_type',1);
        if($res){
            return json(['code'=>200,'msg'=>'指定成功']);
        }else{
            return json(['code'=>100,'msg'=>'指定失败']);
        }
    }
    /***
     * @return \think\response\Json
     *
     */
    public function down2(){
        $task_id = input('param.id');
        $res = Db::name('person_video')->where('person_id',$task_id)->setField('is_type',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
}